<?php

namespace StarCloud\Library\Sdk;

use Framework\DiServices\HttpClient;
use Phalcon\Di;
use StarCloud\Library\Exception;

class Aote
{
    protected $sea_pid = 53;
    protected $sea_token = '4d4d96030e92f477b78a2bead834722e66bc91b63aa64';

    /* @var $httpClient HttpClient */
    private $httpClient = null;
    private $cacheClass;

    public function __construct()
    {
        $this->httpClient = Di::getDefault()->get('httpClient');
        $this->cacheClass = Di::getDefault()->get('redis');
    }

    public function getHeader()
    {
        return [
            'Content-Type' => 'application/json',
            'sea-token' =>  $this->sea_token,
            'sea-pid' =>  $this->sea_pid,
        ];
    }

    public function getBookList($book_name, $page, $size)
    {
        $url = "https://app.yaozhiyu.cn/sea/bookList?book_name={$book_name}&pageNum={$page}&pageSize={$size}";

        $response = $this->httpClient->sendGetRequest($url, [], $this->getHeader());

        $json = json_decode($response->getBody()->getContents(), true);
        if (isset($json['code']) && $json['code'] == 0) {
            foreach ($json['data'] as &$item) {
                $item['content'] = $this->getBookInfo($item['id']);
            }
            return $json;
        }
        return [];
    }

    public static function getBookKey($bookId)
    {
        return "novel_short_book_{$bookId}";
    }


    public function getBookInfo($book_id, $isCache = false)
    {
        $url = "https://app.yaozhiyu.cn/sea/bookInfo?book_id={$book_id}";

        $response = $this->httpClient->sendGetRequest($url, [], $this->getHeader());

        $json = json_decode($response->getBody()->getContents(), true);
        
        Di::getDefault()->get("logger")->info("novel_short_book: " . json_encode($json));
        if (isset($json['code']) && $json['code'] == 0) {
            return $json['data'];
        }
        return '';
    }

    public function createKeyword($book_id, $keyword)
    {
        $url = "https://app.yaozhiyu.cn/sea/createKeywords";

        $body = [
            'book_id' => $book_id,
            'customParams' => $keyword,
        ];

        $response = $this->httpClient->sendPostRequest($url, $body, $this->getHeader());

        $json = json_decode($response->getBody()->getContents(), true);
        Di::getDefault()->get("logger")->info("novel_short : " . json_encode($json));

        if (isset($json['code']) && $json['code'] == 0) {
            return [$json['data']['code'], $json['data']['promotion_id']];
        }

        throw new Exception(9999, $json['msg'] ?? "系统错误");
    }

    public function getOrders($startTime, $endTime, $page, $size)
    {
        $url = "https://app.yaozhiyu.cn/sea/orderList?startTime={$startTime}&endTime={$endTime}&pageNum={$page}&pageSize={$size}";

        $response = $this->httpClient->sendGetRequest($url, [], $this->getHeader());

        $json = json_decode($response->getBody()->getContents(), true);
        var_dump($json, $startTime, $endTime);
        if (isset($json['code']) && $json['code'] == 0) {
            return $json['data'];
        }
        return [];

    }
}